<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>友链管理</title>
<link rel="stylesheet" type="text/css" href="../css/easyui.css">
<link rel="stylesheet" type="text/css" href="../css/icon.css">
<script type="text/javascript" src="../js/jquery-1.7.1.js"></script>
<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
<script type="text/javascript" language="javascript" src="../js/commom.js"></script>
<script type="text/javascript" language="javascript" src="../js/operate.js"></script>
<script type="text/javascript">
	var url;
	$(document).ready(
			function() {
				dataGridInfo("department_data", "datagrid", "",
						"toolbar");
			});
	function editMessage() {
		var row = $('#message_data').datagrid('getSelected');
		if (row) {
			$('#dlg').dialog('open').dialog('setTitle', '查看留言');
			$("#title").get(0).value = row.title;
			$("#department").get(0).value = row.department;
			$("#address").get(0).value = row.address;
			$("#content").get(0).value = row.content;
		}
	}
	//添加部门
	function addDepartment() {
		$('#dlg').dialog('open').dialog('setTitle', '添加部门');
		$('#fm').form('clear');
		url = 'doAdd';
	}
	//保存部门
	function saveDepartment(){
		$('#fm').form('submit', {
			url : url,
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(result) {
				var result = eval('(' + result + ')');
				if (result.errorMsg) {
					$.messager.show({
						title : 'Error',
						msg : result.errorMsg
					});
				} else {
					$('#dlg').dialog('close'); // close the dialog
					$('#department_data').datagrid('reload'); // reload the user data
				}
			}
		});
	}
	
	//修改level后的触发
	function changeLevel(rec) {
		var _url = 'searchByLevel?level=' + rec.value;
		$('#parentId').combobox('reload',_url);						    						   
	}
	
	//删除部门
	function destroyDept() {
		var row = $('#department_data').datagrid('getSelected');
		if (row) {
			$.messager.confirm('Confirm',
				'你确定删除选中部门吗?', function(r) {
					if (r) {
						$.post('checkChildDept', {
							id : row.id
						}, function(result) {
							//如果存在子部门
							if (result.errorMsg) {
								$.messager.confirm('Confirm', result.errorMsg, function(_r) {
									if(_r) {
										doDestroy(row.id);
									} else {
										$('#department_data').datagrid('reload'); // reload the user data
									}
								});								
							} else {
								doDestroy(row.id);
							}
						}, 'json');
					}
			});
		} else {
			$.messager.show({
				title : '提示信息',
				msg : '请选择要操作的数据!'
			});
		}
	}
	//开始删除部门
	function doDestroy(_id) {
		$.post('doDelete',{id : _id}, function(result) {
			if(result.errorMsg) {
				$.messager.show({
					title : 'Error',
					msg : result.errorMsg
				});
			} else {
				$('#department_data').datagrid('reload'); // reload the user data
			}
		});
	}
	//加载edit表单
	function editDept() {
		var row = $('#department_data').datagrid('getSelected');
		if (row) {
			$.post('goUpdate',{id:row.id},function(result){
				if(result.errorMsg) {
					$.messager.show({
						title : 'Error',
						msg : result.errorMsg
					});
				} else {
					$('#dlg').dialog('open').dialog('setTitle', '修改部门信息');
					$('#fm').form('load', result);
					$('#parentId').combobox('reload', 'searchByLevel?level=' + result.level);
					$('#parentId').combobox('setValue',result.parentId);
				}
			});
			url = 'doUpdate?department.id=' + row.id;
		}
	}
</script>
</head>
<body>
	<div class="easyui-panel" data-options="fit:true" style='border: none'>
		<div class="easyui-layout" data-options="fit:true"
			style='border: none'>
			<div id='sss' data-options="region:'center'" style='border: none'>
				<table style='border: 1px solid red' id="department_data"
					class="easyui-datagrid">
					<thead>
						<tr>
							<th data-options="field:'id',width:30,align:'center',hidden:true">编号</th>
							<th data-options="field:'deptId',width:80,align:'center'">部门编号</th>
							<th data-options="field:'name',width:80,align:'center'">部门名称</th>
							<th data-options="field:'level',width:160,align:'center',formatter: function(value,row,index){
								if (row.level>0){
									return row.level + '级目录';
								} else {
									return '1级目录';
								}
							}">部门级别</th>
							<th data-options="field:'parentName',width:160,align:'center',formatter:function(value,row,index){
								if(value) {
									return value;
								} else {
									return '无';
								}
							}">所属部门</th>
						</tr>
					</thead>
				</table>
				<div id="toolbar">
					<a href="javascript:void(0)" class="easyui-linkbutton"
						iconCls="icon-add" plain="true" onclick="addDepartment()">添加部门</a>
					<a href="javascript:void(0)" class="easyui-linkbutton"
						iconCls="icon-remove" plain="true" onclick="destroyDept()">删除部门</a>
					<a href="javascript:void(0)" class="easyui-linkbutton"
						iconCls="icon-edit" plain="true" onclick="editDept()">修改部门</a>
				</div>

				<div id="dlg" class="easyui-dialog"
					style="width: 500px; height: 400px; padding: 10px 20px"
					closed="true" buttons="#dlg-buttons">
					<div class="ftitle">部门信息</div>
					<form id="fm" method="post">
						<div class="fitem">
							<label>部门编号:</label>
							<input id="deptId" name="department.deptId" type='text' style='width:270px;' required="true">
						</div>
						<div class="fitem">
							<label>部门名称:</label> 
							<input id='name' name='department.name' type='text' style='width:270px;' required="true">
						</div>
						<div class="fitem">
							<label>部门级别:</label> 
						    <select id="level" class="easyui-combobox" required="true" name="department.level" style="width:270px;"
						    	data-options="
									onSelect: function(rec){
										changeLevel(rec);
								}">
							    <option value="1">一级部门</option>
							    <option value="2">二级部门</option>
							    <option value="3">三级部门</option>
							    <option value="4">四级部门</option>
							    <option value="5">五级部门</option>
						    </select>
						</div>
						<div class="fitem">
							<label>所属部门:</label>
							<input id="parentId" name="department.parentId" required="true" class="easyui-combobox" style="width:270px;"
								data-options="valueField:'id',textField:'name'">
						</div>
					</form>
				</div>
				<div id="dlg-buttons">
					<a href="javascript:void(0)" class="easyui-linkbutton c6"
						iconCls="icon-ok" onclick="saveDepartment()" style="width: 90px">确定</a>
					<a
						href="javascript:void(0)" class="easyui-linkbutton"
						iconCls="icon-cancel"
						onclick="javascript:$('#dlg').dialog('close')">取消</a>
				</div>
			</div>
		</div>
	</div>
</body>
</html>